package com.example.skyline.controller;


import cn.hutool.json.JSON;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.skyline.common.Result;
import com.example.skyline.entity.Address;
import com.example.skyline.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    // 查询
    @GetMapping ("/select")
    public Object selectAddress(
            @RequestParam(value = "id",defaultValue = "") Integer id,
            @RequestParam(value = "deviceId",defaultValue = "") Integer deviceId

    ){
        List<Address> addressList=addressService.getAddress(id,deviceId);
//        System.out.println(addressList);
        return Result.success(addressList);
    }

    // 增加
    @PostMapping("/add")
    public Object addAddress(@RequestBody Address address) {
        addressService.saveAddress(address);
        return Result.success("地址信息添加成功");
    }

    // 修改
    @PostMapping("/update")
    public Object updateAddress(@RequestBody Address address) {
        addressService.updateAddress(address);
        return Result.success("地址信息修改成功");
    }

    // 删除
    @DeleteMapping ("/delete")
    public Object deleteAddress(@RequestParam Integer id) {
        addressService.deleteAddress(id);
        return Result.success("地址信息删除成功");
    }


    //导出 功能
    @GetMapping("/export")
    public void export(
            HttpServletResponse response
    ) throws Exception {
        List<Address> taskList = addressService.getAddress();
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("name", "设备名称");
        writer.addHeaderAlias("deviceId", "设备id");
        writer.addHeaderAlias("accessProtocol", "接入协议");
        writer.addHeaderAlias("enable", "是否启用");
        writer.addHeaderAlias("transmissionProtocol", "传输协议");
        writer.addHeaderAlias("status", "状态");
        writer.addHeaderAlias("channel", "通道");
        writer.setOnlyAlias(true);
        writer.write(taskList, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("设备信息", "UTF-8" );
        response.setHeader("Content-Disposition","attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream os = response.getOutputStream();
        writer.flush(os);
        writer.close();
    }


    //导入 功能
    @PostMapping("/import")
    public Object importDate(MultipartFile file) throws Exception {
    // 1.拿到输入流 构建的reader
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
    // 2.拿到reader里面的数据
        reader.addHeaderAlias("设备名称","name");
        reader.addHeaderAlias("设备id","deviceId");
        reader.addHeaderAlias("接入协议","accessProtocol");
        reader.addHeaderAlias("是否启用","enable");
        reader.addHeaderAlias("传输协议","transmissionProtocol");
        reader.addHeaderAlias("状态","status");
        reader.addHeaderAlias("通道","channel");
        List<Address> addresses = reader.readAll(Address.class);
        System.out.println("aaaa"+addresses);
     // 3.将数据写到数据库
        for (Address address : addresses) {
            addressService.saveAddress(address);
        }
        return Result.success("地址信息导入成功");
    }





}
